package com.iwork.platform.controller;

import com.iwork.jute.Response;
import com.iwork.platform.service.CaptchaService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@Api(tags = "Captcha Controller")
public class CaptchaController {

    private CaptchaService captcha;

    @Autowired
    public CaptchaController(CaptchaService captcha) {
        this.captcha = captcha;
    }

    /**
     * 图形验证码
     */
    @RequestMapping(value = "/captcha.jpg", method = RequestMethod.GET)
    public void captcha(HttpServletRequest request, HttpServletResponse response) {
        captcha.generate(request, response);
    }

    @RequestMapping(value = "/captcha/validate/{captcha}", method = RequestMethod.GET)
    @ResponseBody
    public Response<Boolean> captchaValidate(@PathVariable("captcha") String captchaStr, HttpServletRequest request, HttpServletResponse response) {
        return Response.ok(captcha.validate(request, response, captchaStr));
    }

}
